//
//  TopicTableViewCell.swift
//  HoneyBee
//
//  Created by point on 2018/1/24.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

class TopicTableViewCell: UITableViewCell {

    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var commentCountLabel: UILabel!
    @IBOutlet weak var groupLabel: UILabel!
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var showImageView: UIImageView!
    @IBOutlet weak var descLabel: UILabel!
    
    @IBOutlet weak var playImageView: UIImageView!
    @IBOutlet weak var imageWidth: NSLayoutConstraint!
    @IBOutlet weak var authorImageView: UIImageView!
    @IBOutlet weak var groupImageView: UIImageView!
    @IBOutlet weak var countImageView: UIImageView!
    @IBOutlet weak var timeImageView: UIImageView!
    
    @IBOutlet weak var deleteBtn: UIButton!
    
    var model:TopicModel?

    var deleteBlock:(() -> ())?
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        authorImageView.image = UIImage.icon(from: kIcon_author, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
        groupImageView.image = UIImage.icon(from: kIcon_group, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
        countImageView.image = UIImage.icon(from: kIcon_comments, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
        timeImageView.image = UIImage.icon(from: kIcon_time, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    func configModel(model:TopicModel , index:IndexPath) {
        self.model = model
        
        if model.lastCommentsTime.count > 0{
            let index = model.lastCommentsTime.index(model.lastCommentsTime.endIndex, offsetBy: -3)
            let lastCommentString = String(model.lastCommentsTime[..<index])
            timeLabel.text =  String.timeStringToShow(str: lastCommentString)
        } else {
            if model.updateTime.count > 0{
                let index = model.updateTime.index(model.updateTime.endIndex, offsetBy: -3)
                let lastUpdateString = String(model.updateTime[..<index])
                timeLabel.text =  String.timeStringToShow(str: lastUpdateString)
            } else {
                if model.createTime.count > 0{
                    let index = model.createTime.index(model.createTime.endIndex, offsetBy: -3)
                    let lastCreateString = String(model.createTime[..<index])
                    timeLabel.text =  String.timeStringToShow(str: lastCreateString)
                } else {
                    timeLabel.text =  String.timeStringToShow(str: "0")
                }
            }
        }
        commentCountLabel.text = String(format: "%d", model.commentsNum)
        if model.groupName.isEmpty {
            groupImageView.isHidden = true
        } else {
            groupImageView.isHidden = false
        }
        groupLabel.text = model.groupName
        
        if model.authorName.isEmpty {
            authorImageView.isHidden = true
        } else {
            authorImageView.isHidden = false
        }
        authorLabel.text = model.authorName
        if let url = URL(string: model.firstImg) {
            showImageView.setImageWith(url, placeholderImage: nil)
            showImageView.layer.cornerRadius = 3
            showImageView.layer.masksToBounds = true
            imageWidth.constant = 100
        } else {
            imageWidth.constant = 0
        }
        descLabel.text = model.title
        
        deleteBtn.isHidden =  model.authorId == UserInfo.shareInstance.userModel().id
        self.playImageView.isHidden = model.videoTime == 0
    }
    
    @IBAction func didClick(_ sender: UIButton) {
        if deleteBlock != nil {
            deleteBlock!()
        }
    }
}
